//
//  HomeNewSiteCell.m
//  WGFGlobalHouse
//
//  Created by waiguofang on 15/4/15.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//    首页中的cell（新盘cell）

#import "HomeNewSiteCell.h"
#import "HomeNewSiteCellModel.h"
#import "UIImageView+WebCache.h"
#import "UserModal.h"
#import "config.h"
#import "FTWCache.h"

#define  ImgW 120/320.0*UIScreenWidth
#define ImgH 100/120.0 * ImgW
#define ImgOrih 100


@interface HomeNewSiteCell ()

-(void)initView;



//楼盘名称
@property (weak, nonatomic) IBOutlet UILabel *titleNameLab;
//开盘时间
@property (weak, nonatomic) IBOutlet UILabel *openTimeLab;
//楼盘的位置
@property (weak, nonatomic) IBOutlet UILabel *addressLab;

//天
@property (weak, nonatomic) IBOutlet UILabel *dayLab;
//小时
@property (weak, nonatomic) IBOutlet UILabel *hourLab;
//分钟
@property (weak, nonatomic) IBOutlet UILabel *minuteLab;
//秒
@property (weak, nonatomic) IBOutlet UILabel *secondLab;



//楼盘图片、或者是适配
@property (weak, nonatomic) IBOutlet UIImageView *siteImage;

//美金
@property (weak, nonatomic) IBOutlet UILabel *dollarLab;
//人民币
@property (weak, nonatomic) IBOutlet UILabel *renMinBiLab;


//写着“最高佣金”字样的lab
@property (weak, nonatomic) IBOutlet UILabel *moenyTxt;
//竖分割线
@property (weak, nonatomic) IBOutlet UIView *Hview;
//显示最高佣金
@property (weak, nonatomic) IBOutlet UILabel *pointLab;


//楼盘标签
@property (weak, nonatomic) IBOutlet UILabel *good1Lab;
@property (weak, nonatomic) IBOutlet UILabel *good2Lab;



@property (strong, nonatomic) IBOutlet UILabel *progressLab1;
@property (strong, nonatomic) IBOutlet UILabel *progressLab2;
@property (strong, nonatomic) IBOutlet UILabel *progressLab3;
@property (strong, nonatomic) IBOutlet UILabel *progressLab4;
@property (strong, nonatomic) IBOutlet UIImageView *progressImgView;


@property (strong, nonatomic) IBOutlet UIView *clockView;
@property (strong, nonatomic) IBOutlet UILabel *finishLab;


@property (copy,nonatomic) NSString *openData;
@property (nonatomic,copy)NSString *expiryDate;
@property (nonatomic,strong) NSTimer *timer;

@property (nonatomic,strong)NSLayoutConstraint *laCons;
@property (weak, nonatomic) IBOutlet UIView *timeView;

@property (nonatomic,strong)NSLayoutConstraint *layout;
@property (weak, nonatomic) IBOutlet UIView *lableView;

@end


@implementation HomeNewSiteCell
@synthesize openData,timer,expiryDate;



+(instancetype) cellWithTableView:(UITableView *)tableview;
{
    static NSString *ID = @"HomeNewSiteCell";
    HomeNewSiteCell *cell = [tableview dequeueReusableCellWithIdentifier:ID];
    if (cell == nil)
    {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"HomeNewSiteCell" owner:nil options:nil]lastObject];
        
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        for (NSLayoutConstraint *constaint in cell.timeView.constraints) {
            
            if (constaint.firstItem == cell.timeView && constaint.firstAttribute == NSLayoutAttributeHeight) {
                
                cell.layout = constaint;
            }
        }
        
        [cell initView];
        
        [cell addTimer];
    }
    
       return cell;
}


//动态计算高度,感觉不同手机的屏幕宽度 和 图片的宽度，对图片坐比例拉伸
+(CGFloat) getHight:(HomeNewSiteCellModel*)data withTableView:(UITableView *)tableView{
    

    static HomeNewSiteCell *cell = nil;
    
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        
        cell = [HomeNewSiteCell cellWithTableView:tableView];
        
    });
    [cell setData:data];
    if ([UserModal shareUserModel].userType == UserTypeCustomer || [UserModal shareUserModel].userType == UserTypeMiddleMan){
        
        return 225 + 1+ImgH - ImgOrih -20;
    }
    return 225 + 1+ImgH - ImgOrih;
    
}



-(void)setData:(HomeNewSiteCellModel*)data{
    
    for (NSLayoutConstraint *constraint in self.siteImage.constraints) {
        
        if (constraint.firstItem == self.siteImage && constraint.firstAttribute ==
            NSLayoutAttributeWidth) {
            constraint.constant = ImgW;
            
            
        }
        
        if (constraint.firstItem == self.siteImage && constraint.firstAttribute ==
            NSLayoutAttributeHeight) {
            constraint.constant = ImgH;
        }
        
        
    }
    
    
    [self.siteImage layoutIfNeeded];

    
//    for (NSLayoutConstraint *constraint in self.siteImage.constraints) {
//        if (constraint.firstItem == self.siteImage && constraint.firstAttribute ==
//            NSLayoutAttributeHeight) {
//            constraint.constant = 200+30;
//        }
//    }
//    [self.siteImage layoutIfNeeded];
    
    self.titleNameLab.text = data.title;
    self.addressLab.text = data.address;
    
    self.openTimeLab.text =[NSString stringWithFormat:@"%@",data.openDate];;
    
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    
    formatter.numberStyle = NSNumberFormatterDecimalStyle;
    
     NSString *totalAmountUSD = [formatter stringFromNumber:[NSNumber numberWithInteger:[data.totalAmountUSD intValue]]];
    
    
    self.dollarLab.text = [NSString stringWithFormat:@"$%@起",totalAmountUSD];;
    self.renMinBiLab.text = data.totalAmountCNY;
    self.pointLab.text = data.maxRate;
    
    [self.siteImage sd_setImageWithURL:[NSURL URLWithString:data.imgUrl] placeholderImage:nil];
    
    if (data.labArr.count>0) {
        self.good1Lab.text = data.labArr.firstObject;
        if(data.labArr.count>1){
            self.good2Lab.text = data.labArr.lastObject;
        }
    }else{
        
        self.good1Lab.text = @"暂无数据";
        self.good2Lab.text = @"暂无数据";
    }
   
    
    switch (data.hourseStauts) {
        case 1:
            
            [self.progressLab1 setTextColor:[UIColor redColor]];
            [self.progressLab2 setTextColor:UIColorFromRGB(0x555555)];
            [self.progressLab3 setTextColor:UIColorFromRGB(0x555555)];
            [self.progressLab4 setTextColor:UIColorFromRGB(0x555555)];
            [self.progressImgView setImage:[UIImage imageNamed:@"xiaoshou_zhuantai_b"]];
        
            
            break;

        case 2:
            
            [self.progressLab1 setTextColor:UIColorFromRGB(0x333333)];
            [self.progressLab2 setTextColor:[UIColor redColor]];
            [self.progressLab3 setTextColor:UIColorFromRGB(0x555555)];
            [self.progressLab4 setTextColor:UIColorFromRGB(0x555555)];
            [self.progressImgView setImage:[UIImage imageNamed:@"xiaoshou_zhuantai_c"]];
            
            break;

        case 3:
            [self.progressLab1 setTextColor:UIColorFromRGB(0x333333)];
            [self.progressLab2 setTextColor:UIColorFromRGB(0x333333)];
            [self.progressLab3 setTextColor:[UIColor redColor]];
            [self.progressLab4 setTextColor:UIColorFromRGB(0x555555)];
            [self.progressImgView setImage:[UIImage imageNamed:@"xiaoshou_zhuantai_d"]];
            

            break;

        case 4:
            
            [self.progressLab1 setTextColor:UIColorFromRGB(0x333333)];
            [self.progressLab2 setTextColor:UIColorFromRGB(0x333333)];
            [self.progressLab3 setTextColor:UIColorFromRGB(0x333333)];
            [self.progressLab4 setTextColor:[UIColor redColor]];
            [self.progressImgView setImage:[UIImage imageNamed:@"xiaoshou_zhuantai_e"]];
            

            
            break;

        default:
            break;
    }
    
    openData = data.openDateWithHour;
    expiryDate = data.expiryDate;
}




//在用户的登录身份是顾客的时候，隐藏“最高佣金”等lab
-(void)initView
{
    
    self.titleNameLab.preferredMaxLayoutWidth = PMW-10-10;
    self.addressLab.preferredMaxLayoutWidth = PMW-10-12-8-10;
    [self.addressLab layoutIfNeeded];
    
    if ([UserModal shareUserModel].userType == UserTypeCustomer || [UserModal shareUserModel].userType == UserTypeMiddleMan) {
        self.moenyTxt.hidden = YES;
        self.pointLab.hidden = YES;
        self.Hview.hidden = YES;
        self.lableView.hidden = YES;
        self.layout.constant = 0;
        self.timeView.hidden = YES;
        [self.contentView layoutIfNeeded];
    }
    else
    {
        self.moenyTxt.hidden = NO;
        self.pointLab.hidden = NO;
        self.Hview.hidden = NO;
        self.lableView.hidden = NO;
        self.layout.constant = 20;
        self.timeView.hidden = NO;
        [self.contentView layoutIfNeeded];
    }
    
    
    [self.dayLab.layer setMasksToBounds:YES];
    [self.dayLab.layer setCornerRadius:2];
    [self.hourLab.layer setMasksToBounds:YES];
    [self.hourLab.layer setCornerRadius:2];
    [self.minuteLab.layer setMasksToBounds:YES];
    [self.minuteLab.layer setCornerRadius:2];
    [self.secondLab.layer setMasksToBounds:YES];
    [self.secondLab.layer setCornerRadius:2];

    self.finishLab.layer.masksToBounds = YES;
    self.finishLab.layer.cornerRadius = 1;
    


    /*
    for (NSLayoutConstraint *constraint in self.progressLab2.superview.constraints) {
        if (constraint.firstItem == self.progressLab2 && constraint.firstAttribute ==
            NSLayoutAttributeLeading&&
            constraint.secondItem == self.progressLab1 && constraint.secondAttribute ==
            NSLayoutAttributeTrailing) {
            
            constraint.constant = 1;
            
        }else if (constraint.firstItem == self.progressLab3 && constraint.firstAttribute ==
                  NSLayoutAttributeLeading&&
                  constraint.secondItem == self.progressLab2 && constraint.secondAttribute ==
                  NSLayoutAttributeTrailing){

            constraint.constant = 1;
        }
    }
    
    [self.progressLab2 layoutIfNeeded];
    [self.progressLab3 layoutIfNeeded];
     */
    
}



/**
 *  倒计时
 */
-(void)timeFireMethod
{
    //2014-04-01 12:00:00
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
//    openData = @"2015-05-06 10:46:00";
    
    NSDate *fireDate=[dateFormatter dateFromString:expiryDate];
    fireDate = [FTWCache getLocData:fireDate];
    
    NSDate *today = [NSDate date];//当前时间
    today = [FTWCache getLocData:today];
    
    
    unsigned int unitFlags = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *d = [calendar components:unitFlags fromDate:today toDate:fireDate options:0];//计算时间差
    
    
    NSInteger day = [d day];
    NSInteger hour = [d hour];
    NSInteger minute = [d minute];
    NSInteger second = [d second];
    
    self.dayLab.text = [NSString stringWithFormat:@"%.2ld ",(long)day];
    self.hourLab.text = [NSString stringWithFormat:@"%.2ld",(long)hour];
    self.minuteLab.text = [NSString stringWithFormat:@"%.2ld",(long)minute];
    self.secondLab.text = [NSString stringWithFormat:@"%.2ld",(long)second];
    
    if (second<0||minute<0||hour<0||day<0) {
        
   
        [self.finishLab setHidden:false];
        [self.clockView setHidden:true];
    }else{
        [self.finishLab setHidden:YES];
        [self.clockView setHidden:false];
    }
}



-(void)addTimer;
{
    if ([UserModal shareUserModel].userType==UserTypeCustomer) {
        
        [self.clockView setHidden:YES];
        [self.finishLab setHidden:YES];
        
    }else{
        
        [self.clockView setHidden:false];
        [self.finishLab setHidden:false];
        
         timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
    }
}







@end
